package com.karina.visualization.chart.skins;

import com.karina.visualization.chart.client.Graphics;
import com.karina.visualization.chart.model.CoordinateChartModel;

public class CoordinatePaneSkinImpl<M extends CoordinateChartModel<?>> extends
		ChartPaneSkinImpl<M> {
	private static final int numHorizontal = 10;

	private final boolean hasGrid;

	public CoordinatePaneSkinImpl(boolean hasGrid) {
		super();
		this.hasGrid = hasGrid;
	}

	@Override
	public void draw(Graphics g) {
		super.draw(g);
		if (hasGrid) {
			g.saveContext();
			drawHorizontalNet(g, numHorizontal);
			drawVerticalNet(g);
			g.restoreContext();
		}
	}

	public void drawHorizontalNet(Graphics g, int numLine) {

		M data = getData();

		// int y = (data.getY()) + data.getMarginBottom();
		int x = data.getX() + data.getMarginLeft();
		int width = (data.getWidth() - data.getMarginLeft());
		int height = data.getHeight() - data.getMarginBottom();
		int gap = height / numLine;

		g.beginPath();
		g.setJointCap("cap");
		g.setGlobalAlpha(0.5);
		while (height >= 0) {
			g.moveTo(x, height);
			g.lineTo(x + width, height);
			height -= gap;
		}
		g.stroke();
		g.endPath();

	}

	public void drawVerticalNet(Graphics g) {

		M data = getData();

		int numLine = data.getXAxisModel().getCategories().size() * 2;

		int y = (data.getY());
		int x = data.getX() + data.getMarginLeft();
		int width = (data.getWidth() - data.getMarginLeft());
		int height = data.getHeight() - data.getMarginBottom();
		int gap = width / numLine;

		g.beginPath();
		g.setGlobalAlpha(0.5);
		g.setJointCap("cap");
		while (width > 0) {
			g.moveTo(x + width, y);
			g.lineTo(x + width, y + height);
			width -= gap;
		}
		g.stroke();
		g.endPath();

	}

}
